package org.jeecg.modules.geo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.core.util.ExportUtil;
import org.jeecg.modules.geo.entity.BimGeoApp;
import org.jeecg.modules.geo.entity.export.ExportBimGeoApp;
import org.jeecg.modules.geo.model.BimGeoAppAuthConfig;
import org.jeecg.modules.geo.service.IBimGeoAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: geo应用管理
 * @Author: jeecg-boot
 * @Date: 2022-11-29
 * @Version: V1.0
 */
@Api(tags = "geo应用管理")
@RestController
@RequestMapping("/geo/bimGeoApp")
@Slf4j
public class BimGeoAppController extends JeecgController<BimGeoApp, IBimGeoAppService> {

    @Autowired
    private IBimGeoAppService bimGeoAppService;

    /**
     * 分页列表查询
     *
     * @param bimGeoApp
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "geo应用管理-分页列表查询", notes = "geo应用管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<BimGeoApp>> queryPageList(BimGeoApp bimGeoApp,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                  HttpServletRequest req) {
        QueryWrapper<BimGeoApp> queryWrapper = QueryGenerator.initQueryWrapper(bimGeoApp, req.getParameterMap());
        queryWrapper.orderByAsc("sort_order");
        Page<BimGeoApp> page = new Page<BimGeoApp>(pageNo, pageSize);
        IPage<BimGeoApp> pageList = bimGeoAppService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param bimGeoApp
     * @return
     */
    @AutoLog(value = "geo应用管理-添加")
    @ApiOperation(value = "geo应用管理-添加", notes = "geo应用管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody @Valid BimGeoApp bimGeoApp) {
        bimGeoAppService.add(bimGeoApp);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param bimGeoApp
     * @return
     */
    @AutoLog(value = "geo应用管理-编辑")
    @ApiOperation(value = "geo应用管理-编辑", notes = "geo应用管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody @Valid BimGeoApp bimGeoApp) {
        bimGeoAppService.update(bimGeoApp);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "geo应用管理-通过id删除")
    @ApiOperation(value = "geo应用管理-通过id删除", notes = "geo应用管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") Integer id) {
        bimGeoAppService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "geo应用管理-批量删除")
    @ApiOperation(value = "geo应用管理-批量删除", notes = "geo应用管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "geo应用管理-通过id查询", notes = "geo应用管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<BimGeoApp> queryById(@RequestParam(name = "id", required = true) String id) {
        BimGeoApp bimGeoApp = bimGeoAppService.getById(id);
        if (bimGeoApp == null) {
            return Result.error("未找到对应数据", null);
        }
        return Result.OK(bimGeoApp);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param bimGeoApp
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BimGeoApp bimGeoApp) {

        QueryWrapper<BimGeoApp> queryWrapper = QueryGenerator.initQueryWrapper(bimGeoApp, request.getParameterMap());
        List<BimGeoApp> bimGeoApps = bimGeoAppService.getBaseMapper().selectList(queryWrapper);

        List<ExportBimGeoApp> exportBimGeoApps = new ArrayList<>();
        for (BimGeoApp geoApp : bimGeoApps) {
            ExportBimGeoApp exportBimGeoApp = new ExportBimGeoApp();
            BeanUtil.copyProperties(geoApp, exportBimGeoApp);
            exportBimGeoApps.add(exportBimGeoApp);
        }

        return ExportUtil.exportXls(ExportBimGeoApp.class, "geo应用管理", exportBimGeoApps);
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BimGeoApp.class);
    }

    /**
     * 编辑验证信息
     */
    @AutoLog(value = "geo应用管理-验证信息编辑")
    @ApiOperation(value = "geo应用管理-验证信息编辑", notes = "geo应用管理-验证信息编辑")
    @RequestMapping(value = "/authConfig/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> editAuthConfig(@RequestBody BimGeoAppAuthConfig bimGeoAppAuthConfig) {
        bimGeoAppService.editAuthConfig(bimGeoAppAuthConfig);
        return Result.OK("编辑成功!");
    }

    @ApiOperation(value = "geo应用管理-通过id查询验证信息", notes = "geo应用管理-通过id查询验证信息")
    @GetMapping(value = "/authConfig/queryById")
    public Result<BimGeoAppAuthConfig> queryAuthConfigById(@RequestParam(name = "appId") Integer appId) {
        BimGeoAppAuthConfig bimGeoAppAuthConfig = bimGeoAppService.getAuthConfig(appId);
        if (bimGeoAppAuthConfig == null) {
            bimGeoAppAuthConfig = new BimGeoAppAuthConfig();
        }
        bimGeoAppAuthConfig.setAppId(appId);
        return Result.OK(bimGeoAppAuthConfig);
    }


}
